package cn.exempale.mscapplication.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;

import com.scwang.smart.refresh.header.MaterialHeader;


import java.util.ArrayList;
import java.util.List;

import cn.exempale.mscapplication.R;
import cn.exempale.mscapplication.databinding.ActivityTaskListBinding;
import cn.exempale.mscapplication.dto.TaskDTO;
import cn.exempale.mscapplication.emnus.DeletedType;
import cn.exempale.mscapplication.emnus.ProgressType;
import cn.exempale.mscapplication.emnus.StatusType;
import cn.exempale.mscapplication.model.db.entity.TaskEntity;
import cn.exempale.mscapplication.model.net.callback.Resource;
import cn.exempale.mscapplication.viewModel.TaskViewModel;
import cn.exempale.mscapplication.vo.ProgressStatusVO;

public class TaskListActivity extends BaseActivity {

    private static final String TAG = "TaskListActivity";

    private ActivityTaskListBinding binding;
    private TaskAdapter adapter;
    private TaskViewModel viewModel;
    private MutableLiveData<Boolean> isEmpty;
    private List<TaskEntity> taskEntityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_task_list);
        binding.setLifecycleOwner(this);
        viewModel = new ViewModelProvider(this).get(TaskViewModel.class);
        isEmpty = new MutableLiveData<>();
        isEmpty.setValue(false);
        binding.setIsEmpty(isEmpty);
        adapter = new TaskAdapter(this);
        binding.taskRecyclerView.setAdapter(adapter);
        // 点击事件
        binding.setClick(this::startNewTask);
        // 列表为空点击事件
        binding.emptyInclude.setClick(this::startNewTask);
        taskEntityList = new ArrayList<>();
        // 刷新样式
        binding.refreshLayout.setRefreshHeader(new MaterialHeader(this));
        binding.refreshLayout.setEnableLoadMore(false);
        binding.refreshLayout.setEnableRefresh(false);
        // 加载全部任务
        viewModel.getAll(DeletedType.normal.getCode())
                .observe(this, taskEntities -> {
                    if (taskEntities == null || taskEntities.size() == 0) {
                        // 无任务
                        isEmpty.setValue(true);
                        binding.refreshLayout.setEnableRefresh(false);
                    } else {
                        isEmpty.setValue(false);
                        adapter.setTasks(taskEntities);
                        taskEntityList = taskEntities;
                        binding.refreshLayout.setEnableRefresh(true);
                    }
                });
//        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
//            executor.execute(() -> {
//                for (TaskEntity taskEntity : taskEntityList) {
//                    // 转写异常
//                    if (taskEntity.getProgressStatus() < 0)
//                        return;
//                    ProgressType progressType = ProgressType.match(taskEntity.getProgressStatus());
//                    // 转写完成
//                    if (progressType == ProgressType.TRANSFER_UPLOAD)
//                        return;
//                    // 更新转写状态
//                    TaskDTO dto = new TaskDTO();
//                    dto.setTask_id(taskEntity.getTaskId());
//                    Resource<ProgressStatusVO> resource = viewModel.getProgress(dto);
//                    if (resource.status == StatusType.ERROR) {
//                        Toast.makeText(getBaseContext(),
//                                dto.getTask_id() + "任务更新失败：" + resource.msg,
//                                Toast.LENGTH_SHORT).show();
//                        runOnUiThread(refreshLayout::finishRefresh);
//                    } else if (resource.status == StatusType.SUCCESS) {
//                        ProgressStatusVO vo = resource.data;
//                        taskEntity.setProgressStatus(vo.getStatus());
//                    }
//                }
//                runOnUiThread(refreshLayout::finishRefresh);
//                taskDAO.update(taskEntityList.toArray(new TaskEntity[0]));
//                viewModel.getAll(DeletedType.normal.getCode());
//            });
//        });
    }

    private void startNewTask(Object o) {
        startActivity(new Intent(this, UploadActivity.class));
    }


}